package net.esj.basic.pojo.op;

import java.util.Collection;
import java.util.Collections;
import java.util.Map;
import java.util.Map.Entry;

import net.esj.basic.core.view.RequestUtils;
import net.esj.basic.pojo.TreeStructure;

public interface AuthFunction extends TreeStructure{

	String getName();
	
	Integer getSort();

	String getUrl();
	
	Map<String,Map<String,String[]>> getRights();
	
	<T extends AuthFunction> Collection<T> getChildren();
	
	public static final class Util{
		
		public static final String analyzeRightUrl(String rightUrl){
			if(rightUrl.indexOf("?")>0){
				return rightUrl.substring(0, rightUrl.indexOf("?"));
			}else{
				return rightUrl;
			}
		}
		
		public static final Map<String, String[]> analyzeRightParams(String rightUrl){
			if(rightUrl.indexOf("?")>0){
				String parStr = rightUrl.substring(rightUrl.indexOf("?")+1);
				Map<String, String[]> map = RequestUtils.parseQueryString(parStr);
				return map;
			}else{
				return Collections.EMPTY_MAP;
			}
		}
		
		public static final boolean checkParams(Map<String, String[]> params,Map requestParams){
			boolean pass = true;
			for(Object o :params.entrySet()){
				Map.Entry<String, String[]> entry = (Entry) o;
				Object v = requestParams.get(entry.getKey());
				if(v!=null && v.getClass().isArray()){
					v = ((Object[])v)[0];
				}
				if(v== null){
					return false;
				}
				boolean has = false;
				for(String str : entry.getValue()){
					if(v.equals(str)){
						has |= true;
					}
				}
				pass &= has;
			}
			return pass;
		}
		
	}
}
